import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';

import 'package:flutter_cookbook/ui/pages/HomePage.dart';

class SplashPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Splash(),
    );
  }
  
}

class Splash extends StatefulWidget {
  
  @override
  State<StatefulWidget> createState() {
    return _SplashState();
  }
  
}

class _SplashState extends State {
  
  Timer _timer;

  int time = 3;
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ConstrainedBox(
        constraints: BoxConstraints.expand(),
        child: Stack(
          alignment: Alignment.center,
          children: [
            FlutterLogo(
              size: 100,
            ),
            Positioned(
              right: 20,
              top: 30,
              child: GestureDetector(
                onTap: () => navigatorHome(),
                child: Chip(
                  label: Text("跳过 $time"),
                ),
              )
            )
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    
    SystemChrome.setEnabledSystemUIOverlays([]);

    _timer = Timer.periodic(Duration(seconds: 1), (timer) {

      setState(() {
        time--;
      });

      if (time == 0) {
        navigatorHome();
      }
    });
  }
  
  void navigatorHome() {
    Navigator.of(this.context).pushAndRemoveUntil(
        new MaterialPageRoute(builder: (context) {
          return HomePage();
        })
        , (route) => false);
  }

  @override
  void dispose() {
    super.dispose();
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top, SystemUiOverlay.bottom]);
    _timer.cancel();
  }
  
}